<?php //pack11class

class AdminObject extends Module {

    public $traceEnabled=true;
    public $objectID=0;
    public $object_save="alert('save')";

    public function setup() {
        $propContainer=new SimpleModule();
        $propContainer->setID("AdminObjectContainer");
        $propContainer->setClass("AdminObjectContainer");
        if($this->param) {
            $object=ObjectsManager::getObject($this->param);
        } else {
            MySQLSet::process("Insert into ".MySQL::TABLE_OBJECTS." (`active`) values ('0');");
            $inserted_id=MySQLSet::$id;
            trace(MySQLSet::getQuery());
            MySQLSet::process("Update ".MySQL::TABLE_OBJECTS." set `time`=`updated` where `id`=".$inserted_id.";");
            $object=ObjectsManager::getCurrentObject($inserted_id);
        }

        $this->objectID=$object->id;

        foreach(PropertiesManager::properties() as $property) {
            if($property->group!="price" && $property->group!="media" && $property->group!="description") {
                $type=TypeManager::getType($property->type);
                if($type) {
                    $typeName=$type->system;
                    $itemClass="AdminProperty_".$typeName;
                    $item=new $itemClass($property);
                    if($property->system=="special") $item->count=10;
                    if($typeName=="label") $item->count=50;
                    $propContainer->addChild($item);
                } else {
                    trace($property);
                }
            }
        }

        $active=new CheckBoxInput;
        $active->property_name="active";
        $active->label=LabelManager::getLabel("object_active_label");
        if($object->active) $active->setChecked();
        $active->events=str_replace('\"', '"', "onChange=\"Javascript:CheckBoxValue(this);\"");
        $active->setClass("ActiveObjectCheckBox");
        $propContainer->addChild($active);

           foreach(PropertiesManager::getGroup(3) as $property) {
               if($property->system!="object_currency") {
                    $text=new TextInput;
                    $text->label=$property->label;
                    if($property->value) $text->setBody($property->value);
                    $text->property_name=$property->system;
                    $text->setID("price_".$property->system);
                    $text->events='onkeydown="f(this)" onkeyup="f(this)" onblur="f(this)" onclick="f(this)"';
                    $propContainer->addChild($text);
               } else {
                    $propContainer->addChild($this->generateCurrencySelect());
               }
            }


        /*$spacer=new SimpleModule();
        $spacer->setClass("MapSizer");
        $propContainer->addChild(new ObjectCardMap($object));
        $propContainer->addChild($spacer);*/

        $this->addChild($propContainer);

        $longTextContainer=new SimpleModule();
        $longTextContainer->setClass("longTexts");

        $languages=LanguagesManager::getLanguages();
        foreach($languages as $language) {
            $field=new TextAreaInput();
            $field->label=$language->label;
            $field->id="NVP_".$language->system;
            $field->setClass("LongTextInput");
            $field->property_name="text".$language->id;
            $field->setBody($object->description[$language->id]);
            //$field->events='onkeydown="ValuesManager.countText(this)" onkeyup="ValuesManager.countText(this)" onblur="ValuesManager.countText(this)" onclick="ValuesManager.countText(this)"';
            $CKEditorAddon = new AdminObjectCKeditorAddon();
            $CKEditorAddon->property_name="text".$language->id;
            $field->ta_class="ckeditor";
            $field->extra = Template::html($CKEditorAddon);
            $longTextContainer->addChild($field);

            /*$field=new TextEditor();
            $field->build("NVP_".$language->system);
            $field->id="NVP_".$language->system;
            $field->label=$language->label;
            $field->setClass("LongTextInput");
            $field->setBody($object->description[$language->id]);
            $longTextContainer->addChild($field);*/
        }

       $longTextContainer->addChild(new ObjectCardMap($object));

        $this->addChild($longTextContainer);

        $photos = new SimpleModule;
        $photosData = PropertiesManager::getGroup(4);
        usort($photosData, array(__CLASS__,"sortPhotos"));
        //usort($photosData,"strnatcmp");
            foreach($photosData as $property) {
                $type=TypeManager::getType($property->type);
                if($type) {
                    $typeName=$type->system;
                    $itemClass="AdminProperty_".$typeName;
                    $item=new $itemClass($property);
                    $item->label=$property->label;
                    $item->object=$object->id;
                    $item->num=$property->id;
                    $item->property_name=$property->system;
                    if(is_file(ImageUploader::$smallPath.$property->value)) $item->photo=$property->value;
                    if(is_file(ImageUploader::$largePath.$property->value)) $item->photo_value=$property->value;
                    $photos->addChild($item);
                } else {
                    trace($property);
                }
            }

        $photos->setClass("PhotosContainer");
        $photos->setID("PhotosContainer");
        $this->addChild($photos);

        $queryObject=new GetQueryObject;

        $sizer=new SimpleModule();
        $sizer->addStyle("clear","left");
        $sizer->addStyle("position","relative");
        $this->addChild($sizer);

        $objectSetting=SettingsManager::getSetting("object");
        $obj = clone $objectSetting;
        $obj->value=$object->id;
        $queryObject->setValue($obj, GetQueryObject::SETTING);

        $saveSetting=SettingsManager::getSetting("save");
        $save = clone $saveSetting;
        $save->value=1;
        $queryObject->setValue($save, GetQueryObject::SETTING);

        $this->object_save="?state=".GetQuery::generate($queryObject)."&admin";
    }

    public static function sortPhotos(Property $a, Property $b) {
        //return (0+str_replace("photo","",$a->system))>(0+str_replace("photo","",$b->system));
        //return ($a->label>$b->label);
        return strnatcmp($a->label,$b->label);
    }

    private function generateCurrencySelect() {
        $select=new SelectInput;
        $currencies=CurrencyManager::getCurrencies();
        if(!$currencies) {
            return false;
        }
        foreach($currencies as $currency) {
            $option=new SelectOption;
            $option->label=$currency->label;
            $option->value=$currency->id;
            $option->selected=($currency->id==PropertiesManager::getProperty("object_currency")->value)? SelectOption::SELECTED:"";
            $select->addChild($option);
        }


        $select->property_name="object_currency";
        $select->setID("price_currency");
        $select->label=LabelManager::getLabel("search_currency_label");
        //$select->events=str_replace('\"', '"', "onChange=\"TablePropertiesPopup.setCurrency(this,true);\"");
        $select->class="select";
        //$select->setClass("ObjectsListProperties");
        $select->lamp_class="hidden";
        return $select;
    }

}
?>
